/**
 * Created by weibo on 15-8-3.
 */

var app = angular.module('statisticcontroller', []);

app.controller('StatisticCtrl', function($scope, $window, $filter, StatisticService, ProvinceService, HospitalService) {

  $scope.$watch('start', function (newValue) {
    $scope.start = $filter('date')(newValue, 'yyyy/MM/dd');
  });

  $scope.$watch('end', function (newValue) {
    $scope.end = $filter('date')(newValue, 'yyyy/MM/dd');
  });

  $scope.provinces = ProvinceService.list(function(data) {
    $scope.provinces = data.provinces
  });

  $scope.provinceChanged = function(provinceId) {
    $scope.hospitals = HospitalService.list_by_province(provinceId, function(res) {
      if (res.result == false) {
        $scope.error_msg = res.message;
      }
      $scope.hospitals = res.hospitals;
    });
  }

  $scope.test = function() {
    console.log('hell oworo');
  }

  $scope.statistic_doctor = function() {
      var province = $scope.province;
      var hospital = $scope.hospital;
      var start = $scope.start;
      var end = $scope.end;

      if (!($window.sessionStorage.role == 4) && !province) {
        alert('请选择省份');
        return;
      }

      if (!($window.sessionStorage.role == 4) &&!hospital) {
        alert('请选择医院');
        return;
      }
      if (!start) {
        alert('请选择起始时间');
        return;
      }

      if (!end) {
        alert('请选择结束时间');
        return;
      }


      console.log('province:' + province +", hospital:" + hospital + ", start:" + start + ", end:" + end);

      StatisticService.statistic_doctor(province, start, end, hospital, function(res) {
          $scope.displayed = res.data;
      });
  }

  $scope.statistic_hospital = function() {
      var province = $scope.province;
      var hospital = $scope.hospital;
      var start = $scope.start;
      var end = $scope.end;

      StatisticService.statistic_hospital(province, start, end, hospital, function(res) {
          $scope.displayed = res.data;
      });
  }

  $scope.role = $window.sessionStorage.role;

});
